/**
 * @LogFormat.as
 * 
 * @author sodaChen mail:sujun10#21cn.com
 * @version 1.0
 * <br>Copyright (C), 2010-2015 asFrame.com
 * <br>This program is protected by copyright laws.
 * <br>Program Name:ASFrame Log
 * <br>Date:2011-9-5
 */
package com.asframe.log
{
	import com.asframe.collections.lists.IList;
	import com.asframe.collections.maps.HashMap;
	import com.asframe.log.format.IObjectFormat;
	import com.asframe.utils.ClassUtils;

	/**
	 *
	 * @author sodaChen
	 * Date:2011-9-5
	 */
	public class LogFormat
	{
		/** 对象格式处理集合 **/
		protected var objFormatMap:HashMap;
		
		public function setObjectFormatMap(objFormatMap:HashMap):void
		{
			this.objFormatMap = objFormatMap;
		}
		public function formatObject(obj:Object,needType:Boolean):String
		{
			if(obj == null)
			{
				return "";
			}
			if(obj is String)
			{
				if(needType)
					return "(String:" + obj + ")";
				return obj.toString();
			}
			if(obj is int)
			{
				if(needType)
					return "(int:" + obj + ")";
				return obj.toString();
			}
			if(obj is Number)
			{
				if(needType)
					return "(Number:" + obj + ")";
				return obj.toString();
			}
			if(obj is Boolean)
			{
				if(needType)
					return "(Boolean:" + obj + ")";
				return obj.toString();
			}
			if(obj is uint)
			{
				if(needType)
					return "(uint:" + obj + ")";
				return obj.toString();
			}
			if(obj is Array || obj is Vector || obj is IList)
			{
				var path:String = ClassUtils.getFullyQualifiedName(obj);
				
				
				if(obj is IList)
					obj = IList(obj).getContainer();
				
				var len:int = obj.length;
				var msg:String = "/////////////////[" + path + "]//////////" + "长度:" + len + "{\n";
				var value:* = null;
				for(var i:int = 0; i < len; i++)
				{
//					var str:String = formatObject(obj[i],true);
					msg = msg + formatObject(obj[i],true) + "\n";
//					msg = msg + str + "\n";
				}
				msg = msg + "}///////////////////////////////////////////";
				return msg;
			}
			if(obj is Error)
			{
				var error:Error = obj as Error;
				return error.getStackTrace();
			}
			return toObjectFormat(ClassUtils.getFullyQualifiedName(obj),obj);
		}
		/**
		 * 
		 * @param key
		 * @param obj
		 * @return 
		 */
		private function toObjectFormat(classPath:String,obj:Object):String
		{
			var objFormat:IObjectFormat = objFormatMap.get(classPath) as IObjectFormat;
			if(objFormat == null)
			{
				return obj.toString();
			}
			return objFormat.toFormatString(classPath,obj);
		}
	}
}